package com.yuch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuch.commons.R;
import com.yuch.entity.Brand;
import com.yuch.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api")
public class BrandController {

    @Autowired
    BrandService brandService;

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/getall")
    public R query(Integer pageSize, Integer currentPage, Brand brand) {
        log.info(pageSize + "=====" + currentPage + "=====" + brand);
        LambdaQueryWrapper<Brand> query = new LambdaQueryWrapper<>();
        query.eq(!StringUtils.isEmpty(brand.getStatus()), Brand::getStatus, brand.getStatus());
        query.eq(!StringUtils.isEmpty(brand.getBra()), Brand::getBra, brand.getBra());
        query.eq(!StringUtils.isEmpty(brand.getCompany()), Brand::getCompany, brand.getCompany());
        Page<Brand> brandPage = new Page<>(currentPage, pageSize);
        Page<Brand> page = brandService.page(brandPage, query);
        return R.yes(page);
    }

    /**
     * 按照id查询
     *
     * @return
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        log.info("查询的id---->" + id);
        return R.yes(brandService.getById(id));
    }

    //删除
    @DeleteMapping("/delete")
    public R delete(@RequestParam List<Integer> ids) {
        log.info("删除的ids" + ids);
        brandService.removeByIds(ids);
        return R.yes(null);
    }

    /**
     * 新增或者编辑修改的
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Brand brand) {
        brandService.save(brand);
        return R.yes(null);

    }

    /**
     * 更新
     * @param brand
     * @return
     */
    @PutMapping("/update")
    public R update(@RequestBody Brand brand){
        brandService.updateById(brand);
        return R.yes(null);

    }



}
